# -*- coding:utf-8 -*-
# 面试python
# date: 2016/08/22

# 1. 新旧类的区别;
class NewClass(object):
    _sigleton = {}

    def __init__(self):
        self.x = 'a'
        self.y = 'b'
        super(NewClass, self).__init__()
        print "__init__"

    @staticmethod
    def __new__(cls, *more):
        print "__new__"
        if not cls._sigleton.has_key(cls):
            cls._sigleton[cls] = super(NewClass, cls).__new__(cls, *more)
        return cls._sigleton[cls]

    def my_print(self):
        print self.x





c = NewClass()
print id(c)
print c.__dict__

d = NewClass()
print id(d)


# print dir(NewClass)


def singleton(cls, *args, **kw):
    instances = {}

    def get_instance():
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return get_instance

